<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class="isFullScreen ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
// import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },
  //   methods: {
  //     toggleScreen() {
  //       if (!screenfull.isEnabled) {
  //         this.$notification.open({
  //           message: '温馨提示',
  //           description:
  //             '您的浏览器无法使用全屏功能，请更换谷歌浏览器或者请手动点击F11按钮全屏展示！',
  //           duration: 10,
  //           placement: 'bottomLeft'
  //         })
  //         return false
  //       }
  //       screenfull.toggle()
  //       if (screenfull.isFullScreen) {
  //         this.screenfullBut = false
  //       } else {
  //         this.screenfullBut = true
  //       }
  //     }
  //   }
  // }
  mounted() {
    document.addEventListener('fullscreenchange', e => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏
      // 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isFullScreen = false
      }
    })
    document.addEventListener('webkitfullscreenchange', e => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏
      // 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isFullScreen = false
      }
    })
    document.addEventListener('mozfullscreenchange', e => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏
      // 如果已退出全屏 把本地状态修改为false
      const isFull = document.fullscreenElement
      if (!isFull) {
        this.isFullScreen = false
      }
    })
  },
  methods: {
    toggleScreen() {
      const fn = document.documentElement.webkitRequestFullScreen || document.documentElement.mozfullscreenchange || document.documentElement.fullscreenchange

      this.isFullScreen = !this.isFullScreen
      if (this.isFullScreen === true) {
        // 这里有兼容性问题
        // 这里如果没有指定的this指向,这个的this就会指向window
        fn.call(document.documentElement)
      } else {
        document.exitFullscreen()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
